package com.wzj.uarmor.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wzj.uarmor.domain.User;
import com.wzj.uarmor.service.IUserService;
import com.wzj.uarmor.utils.CommonConst;
import com.wzj.uarmor.utils.ResponseStatus;
import com.wzj.uarmor.utils.ResponseVO;
import com.wzj.uarmor.utils.ResultUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理
 *
 * @author whong
 * @date 2023/03/18
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final IUserService userService;

    /**
     * 添加用户
     *
     * @param user 用户
     * @return {@link ResponseVO}
     */
    @PostMapping("/add")
    public ResponseVO add(@RequestBody User user){
        User existUser = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUserName, user.getUserName()));
        if(existUser != null){
            return ResultUtil.error(ResponseStatus.USER_EXIST);
        }
        userService.save(user);
        return  ResultUtil.success(ResponseStatus.SUCCESS);
    }

    /**
     * 修改权限
     *
     * @param user 用户
     * @return {@link ResponseVO}
     */
    @PostMapping("/editAuth")
    public ResponseVO editAuth (@RequestBody User user){
        User existUser = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUserName, user.getUserName()));
        if(existUser == null){
            return ResultUtil.error(ResponseStatus.USER_NOT_EXIST);
        }
        if(StrUtil.isBlank(user.getAuthority())|| (!CommonConst.AUTHORITY_R.equals(user.getAuthority()) && !CommonConst.AUTHORITY_RW.equals(user.getAuthority()))){
            return  ResultUtil.error("权限不存在");
        }
        existUser.setAuthority(user.getAuthority());
        userService.updateById(existUser);
        return  ResultUtil.success(ResponseStatus.SUCCESS);
    }

    /**
     * 修改密码
     * @param user 用户
     * @return {@link ResponseVO}
     */
    @PostMapping("/editPassWord")
    public ResponseVO editPassWord (@RequestBody User user){
        User existUser = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUserName, user.getUserName()));
        if(existUser == null){
            return ResultUtil.error(ResponseStatus.USER_NOT_EXIST);
        }
        if(StrUtil.isBlank(user.getPassword())){
            return  ResultUtil.error("密码不能为空");
        }
        existUser.setPassword(user.getPassword());
        userService.updateById(existUser);
        return  ResultUtil.success(ResponseStatus.SUCCESS);
    }

    /**
     * 用户列表
     *
     * @return {@link ResponseVO}
     */
    @GetMapping("/list")
    public ResponseVO list(){
        return  ResultUtil.success(userService.list());
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public ResponseVO deletedById(@RequestParam("id")Integer id){
        userService.removeById(id);
        return  ResultUtil.success(ResponseStatus.SUCCESS);
    }

}
